package com.mt.demo.service.impl;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.mt.demo.domain.MtMeeting;
import com.mt.demo.domain.MtMember;
import com.mt.demo.manager.MeetingRoomManager;
import com.mt.demo.manager.MeetingUserManager;
import com.mt.demo.service.IMtMeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MeetingSyncService {
    @Autowired
    private SocketIOServer socketIOServer;

    @Autowired
    private MeetingRoomManager meetingRoomManager;

    @Autowired
    private MeetingUserManager meetingUserManager;

    @Autowired
    private IMtMeetingService meetingService;

    // 每2分钟同步一次会议状态
    @Scheduled(fixedRate = 120000)
    public void syncMeetingStatus() {
        // 获取所有连接的客户端
        Set<String> activeRooms = socketIOServer.getAllClients()
                .stream()
                .map(client -> client.getHandshakeData().getSingleUrlParam("roomId"))
                .filter(roomId -> roomId != null && !roomId.isEmpty())
                .collect(Collectors.toSet());

        // 同步每个房间的状态
        for (String roomId : activeRooms) {
            syncRoomStatus(roomId);
        }
    }

    // 同步单个房间的状态
    public void syncRoomStatus(String roomId) {
        // 获取房间内的所有客户端
        Set<SocketIOClient> roomClients = meetingRoomManager.getRoomClients(roomId);
        
        // 获取房间内的所有用户ID
        List<String> connectedUserIds = roomClients.stream()
                .map(client -> client.getHandshakeData().getSingleUrlParam("userId"))
                .filter(userId -> userId != null && !userId.isEmpty())
                .collect(Collectors.toList());

        // 同步用户状态
        meetingUserManager.syncMeetingUsers(roomId, connectedUserIds);

        // 获取最新的会议信息
        MtMeeting meeting = meetingService.selectMtMeetingByMeetingId(Long.parseLong(roomId));
        
        // 获取在线用户列表
        List<MtMember> onlineUsers = meetingUserManager.getOnlineUsers(roomId);

        // 构建会议状态数据
        MeetingStatusData statusData = new MeetingStatusData();
        statusData.setMeeting(meeting);
        statusData.setOnlineUsers(onlineUsers);
        statusData.setScreenSharingUser(meetingRoomManager.getScreenSharingUser(roomId));
        statusData.setOnlineCount(onlineUsers.size());

        // 广播会议状态给房间内所有用户
        for (SocketIOClient client : roomClients) {
            client.sendEvent("meetingStatusSync", statusData);
        }
    }

    // 会议状态数据类
    private static class MeetingStatusData {
        private MtMeeting meeting;
        private List<MtMember> onlineUsers;
        private String screenSharingUser;
        private int onlineCount;

        // Getters and setters
        public MtMeeting getMeeting() {
            return meeting;
        }

        public void setMeeting(MtMeeting meeting) {
            this.meeting = meeting;
        }

        public List<MtMember> getOnlineUsers() {
            return onlineUsers;
        }

        public void setOnlineUsers(List<MtMember> onlineUsers) {
            this.onlineUsers = onlineUsers;
        }

        public String getScreenSharingUser() {
            return screenSharingUser;
        }

        public void setScreenSharingUser(String screenSharingUser) {
            this.screenSharingUser = screenSharingUser;
        }

        public int getOnlineCount() {
            return onlineCount;
        }

        public void setOnlineCount(int onlineCount) {
            this.onlineCount = onlineCount;
        }
    }
}
